import {sign} from "@/utils/util.js"
import store from "@/core/store.js"
// 全局请求封装
// const base_url = 'http://www.zhongxiujianshe.com'
 // const base_url = 'http://192.168.1.201:7015/chongqingzhaopin'
const base_url = 'https://api.zhifengkeji.cn/chongqingzhaopin'
// const base_url = 'https://k31d501265.oicp.vip/chongqingzhaopin'
// const base_url = 'http://192.168.1.14:7015/chongqingzhaopin'

// 请求超出时间
const timeout = 5000

// 需要修改token，和根据实际修改请求头
export default (params) => {
	console.log(params)
	let url = params.url;
	let method = params.method || "get";
	let data = params.data || {};
	let token="";
	if(store.state.token){
		token=store.state.token;
	}
	if(store.state.youkeType==1){
		token=store.state.ykqiyeToken;
	}
	if(store.state.youkeType==2){
		token=store.state.ykseekerToken;
	}
	let header = {
		'token': token,
		// 'token':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJndWxpLXVzZXIiLCJpYXQiOjE3MjU2ODIxMTEsImlkIjo4LCJuaWNrbmFtZSI6IjE4NTMxOTc5MTAxIiwicm9sZSI6IkNPTVBBTlkifQ.DVn5qwOJ19MHfnRJXdthHxMdWqcGuDN8lHiDuD7NozxMKj2HHrb4vBgRrjlbGzHE_E5rRzhY3AnvQ5x-vKiXQw',
		// 'Content-Type': 'application/json;charset=UTF-8',
		// 'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
		// 'Tenant-Id': uni.getStorageSync('tenantId') || 'xxx', // avue配置相关
	}
	// 封装验证码
	if(params.sign){
		let dist = sign(data);
		console.log(dist)
		for(let key in dist){
			header[key] = dist[key];
		}
	}
	if (params.dataType == "formData") {
		// header['Content-Type'] = 'application/json'
		header['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
		let formData = "";
		for(let key in data){
			formData+=`${key}=${data[key]}&`
		}
		if(formData) data = formData.slice(0,formData.length-1);
	}
	if(params.header && Object.keys(params.header).length>0){
		for(let key in params.header){
			header[key] = params.header[key];
		}
	}
	console.log(header)
	return new Promise((resolve, reject) => {
		uni.request({
			url: base_url + url,
			method: method,
			header: header,
			data: data,
			timeout,
			success(response) {
				const res = response;
				if(!res.data.code==20000){
					wx.showToast({
						title: '未知异常',
						duration: 2000
					})
					reject("请求错误");
				}
				// 根据返回的状态码做出对应的操作
				//获取成功
				// console.log(res.statusCode);
				if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					uni.clearStorageSync()
					switch (res.statusCode) {
						case 401:
							uni.showModal({
								title: "提示",
								content: "请登录",
								showCancel: false,
								success() {
									setTimeout(() => {
										uni.navigateTo({
											url: "/pages/login/index",
										})
									}, 1000);
								},
							});
							break;
						case 404:
							uni.showToast({
								title: '请求地址不存在...',
								duration: 2000,
							})
							break;
						default:
							uni.showToast({
								title: '请重试...',
								duration: 2000,
							})
							break;
					}
				}
			},
			fail(err) {
				if (err.errMsg.indexOf('request:fail') !== -1) {
					wx.showToast({
						title: '网络异常',
						icon: "error",
						duration: 2000
					})
				} else {
					wx.showToast({
						title: '未知异常',
						duration: 2000
					})
				}
				reject(err);

			},
			complete() {
				// 不管成功还是失败都会执行
				// uni.hideLoading();
				uni.hideToast();
			}
		});
	}).catch(() => {});
};